package com.zhiyou100.video.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhiyou100.video.model.Admin;
import com.zhiyou100.video.service.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService as;
	
	@RequestMapping("/login.do")
	public String login() {
		return "forward:/backstage/login.jsp";
	}
	
	@RequestMapping("/login_video")
	public String adminlogin(@RequestParam("login_name") String login_name,@RequestParam("login_pwd") String login_pwd,HttpServletResponse response,HttpSession session){
		System.out.println(login_name);
		Map<String, Object> adminmaps = new HashMap<>();
		adminmaps.put("login_name", login_name);
		adminmaps.put("login_pwd", DigestUtils.md5Hex(login_pwd));
		Admin admin = as.selectAdmin(adminmaps);
		if(admin == null){
			session.setAttribute("message", "用户不存在或者密码错误！请重新输入");
			return "forward:/backstage/login.jsp";
		}else{
			session.setAttribute("admin", admin);
		    return "forward:/admin/video";
		}
	}
	
	@RequestMapping("/logout")
	public String adminlogout(HttpSession session) {
		session.removeAttribute("_front_user");
		return "redirect:/backstage/login.jsp";
	 }
	
}
